﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System.ComponentModel;
using NetJsWire.Lite.Converters;
using Newtonsoft.Json;

namespace NetJsWire.Lite.Widgets.UI.Data
{
    [JsonObject( MemberSerialization = MemberSerialization.OptIn )]
    public class JsonStore : Store
    {
        public JsonStore() : this( null ) { }

        ///<summary>
        ///  Initializes a new instance of Store and adds it to the Container.
        ///</summary>
        ///<param name = "container"></param>
        public JsonStore( IContainer container )
            : base( container )
        {
            base.Xtype = "jsonstore";
            IdProperty = "id";
            SuccessProperty = "success";
            TotalProperty = "total";
        }

        [JsonProperty( "idProperty", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore )]
        [DefaultValue( "id" )]
        [Description( "Name of the property within a row object that contains a record identifier value. Defaults to id" )]
        public string IdProperty { get; set; }

        [JsonProperty( "root", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore )]
        [DefaultValue( ( string ) null )]
        [Description( "Required. The name of the property which contains the Array of row objects. Defaults to undefined. An exception will be thrown if the root property is undefined. The data packet value for this property should be an empty array to clear the data or show no data." )]
        public string Root { get; set; }

        [JsonProperty( "successProperty", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore )]
        [DefaultValue( "success" )]
        [Description( "Name of the property from which to retrieve the success attribute. Defaults to success." )]
        public string SuccessProperty { get; set; }

        [JsonProperty( "totalProperty", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore )]
        [DefaultValue( "total" )]
        [Description( "Name of the property from which to retrieve the total number of records in the dataset. This is only needed if the whole dataset is not passed in one go, but is being paged from the remote server. Defaults to total." )]
        public string TotalProperty { get; set; }

        [JsonProperty( "xtype", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Include )]
        [DefaultValue( "directstore" )]
        [TypeConverter( typeof( XtypeTypeConverter ) )]
        public override string Xtype
        {
            get
            {
                return base.Xtype;
            }
            set
            {
                base.Xtype = value;
            }
        }
    }
}
